package com.appsky.drift.api;

import java.util.Collection;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class UserInfo {

	/**
	 * 用户位置：纬度
	 */
	private int latitude;

	/**
	 * 用户位置：经度
	 */
	private int longitude;

	/**
	 * 用户昵称 默认:无名氏
	 */
	private String nick;

	/**
	 * 用户唯一ID
	 */
	private int userId;

	/**
	 * 1:男 0:女 2:保密 默认保密
	 */
	private int sex;

	/**
	 * 用户所在聊天室的ID，实际上是圈主的ID
	 */
	private int roomId = -1;

	/**
	 * 用户所在聊天室的在线人的列表
	 */
	private HashMap<Integer, UserInfo> RoomPersonMap = new HashMap<Integer, UserInfo>();

	/**
	 * 用户周围在线的人的列表
	 */
	private HashMap<Integer, UserInfo> nearbyPersonMap = new HashMap<Integer, UserInfo>();

	public UserInfo() {
		
	}

	/**
	 * 是否在漂浮着，就是既没创聊天室，又没加入聊天室
	 * 
	 * @return
	 */
	public boolean isDrifting() {
		return (roomId == -1);
	}
	
	public boolean isNotDrifting() {
		return !(isDrifting());
	}

	/**
	 * 是否为聊天室的主人
	 * 
	 * @return
	 */
	public boolean isRoomOwner() {
		return (userId == roomId);
	}

	/**
	 * 是否加入了聊天室
	 * 
	 * @return
	 */
	public boolean isInOtherRoom() {
		return (!this.isDrifting() && !this.isRoomOwner());
	}

	/**
	 * 解析登录或注册时返回的json
	 * 
	 * @param jsonString
	 * @return
	 */
	public int parseLoginJson(String jsonString) {
		int result = Utils.LOGIN_ERROR;
		JSONObject json = JSONObject.parseObject(jsonString);
		if ("nearby_person".equals(json.getString("type"))) {
			result = Utils.LOGIN_SUCCESS;
			JSONObject jsonObject = json.getJSONObject("data");
			int id = jsonObject.getIntValue("id");
			if (this.getUserId() == -1) {
				this.setUserId(id);
				result = Utils.REGISTER_SUCCESS;
			}
			parseNearbyPerson(jsonObject.getJSONArray("person_list"));
		}
		return result;
	}

	/**
	 * 解析用户列表，并保存
	 * 
	 * @param jsonArray
	 */
	public void parseNearbyPerson(JSONArray jsonArray) {
		if (jsonArray.size() != 0 || jsonArray != null) {
			this.clearNearbyPersons();
			int size = jsonArray.size();
			JSONObject json = null;
			synchronized (this) {
				for (int i = 0; i < size; i++) {
					UserInfo item = new UserInfo();
					json = jsonArray.getJSONObject(i);
					item.setUserId(json.getIntValue("id"));
					item.setNick(json.getString("nick"));
					item.setSex(json.getIntValue("sex"));
					item.setLatitude(json.getIntValue("latitude"));
					item.setLongitude(json.getIntValue("longitude"));
					item.setRoomId(json.getIntValue("room_id"));
					this.addNearbyPerson(item);
				}
			}
		}
	}

	/**
	 * 解析用户列表，并保存
	 * 
	 * @param jsonArray
	 */
	public void parseRoomPerson(JSONArray jsonArray) {
		if (jsonArray.size() != 0 || jsonArray != null) {
			this.clearRoomPersons();
			int size = jsonArray.size();
			JSONObject json = null;
			synchronized (this) {
				for (int i = 0; i < size; i++) {
					UserInfo item = new UserInfo();
					json = jsonArray.getJSONObject(i);
					item.setUserId(json.getIntValue("id"));
					item.setNick(json.getString("nick"));
					item.setSex(json.getIntValue("sex"));
					item.setLatitude(json.getIntValue("latitude"));
					item.setLongitude(json.getIntValue("longitude"));
					item.setRoomId(json.getIntValue("room_id"));
					this.addRoomPerson(item);
				}
			}
		}
	}

	public int getLatitude() {
		return latitude;
	}

	public void setLatitude(int latitude) {
		this.latitude = latitude;
	}

	public int getLongitude() {
		return longitude;
	}

	public void setLongitude(int longitude) {
		this.longitude = longitude;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String userNick) {
		this.nick = userNick;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int userSex) {
		this.sex = userSex;
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int inChatRoomId) {
		this.roomId = inChatRoomId;
	}

	public HashMap<Integer, UserInfo> getRoomPersons() {
		return RoomPersonMap;
	}

	public UserInfo getUserFromNearbyPerson(int id) {
		return this.nearbyPersonMap.get(id);
	}
	
	public void addRoomPerson(UserInfo userInfo) {
		this.RoomPersonMap.put(userInfo.getUserId(), userInfo);
	}
	
	public void removeRoomPerson(UserInfo userInfo) {
		this.RoomPersonMap.remove(userInfo.getUserId());
	}

	public void clearRoomPersons() {
		this.RoomPersonMap.clear();
	}

	public HashMap<Integer, UserInfo> getNearbyPersons() {
		return nearbyPersonMap;
	}
	
	public Collection<UserInfo> getNearbyPersonListFromHashMap() {
		return nearbyPersonMap.values();
	}
	
	public Collection<UserInfo> getRoomPersonListFromHashMap() {
		return RoomPersonMap.values();
	}

	public void addNearbyPerson(UserInfo userInfo) {
		this.nearbyPersonMap.put(userInfo.getUserId(), userInfo);
	}
	
	public UserInfo addNearbyPerson(JSONObject json) {
		UserInfo user = new UserInfo();
		user.setUserId(json.getIntValue("id"));
		user.setNick(json.getString("nick"));
		user.setSex(json.getIntValue("sex"));
		user.setLatitude(json.getIntValue("latitude"));
		user.setLongitude(json.getIntValue("longitude"));
		this.addNearbyPerson(user);
		return user;
	}
	
	public UserInfo removeNearbyPerson(int id) {
		return this.nearbyPersonMap.remove(id);
	}

	public void clearNearbyPersons() {
		this.nearbyPersonMap.clear();
	}

}